package com.ppc.ugo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ppc.ugo.dao.mapper.GoodMapper;
import com.ppc.ugo.dao.pojo.Good;
import com.ppc.ugo.dao.pojo.GoodAttrs;
import com.ppc.ugo.dao.pojo.GoodPics;
import com.ppc.ugo.service.GoodAttrsService;
import com.ppc.ugo.service.GoodPicsService;
import com.ppc.ugo.service.GoodService;
import com.ppc.ugo.vo.*;
import com.ppc.ugo.vo.param.GoodParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
@Slf4j
public class GoodServiceImpl implements GoodService {
    @Resource
    private GoodMapper goodMapper;
    @Autowired
    private GoodPicsService goodPicsService;
    @Autowired
    private GoodAttrsService goodAttrsService;

    @Override
    public void addGood(Good good) {
        goodMapper.insert(good);
        log.info("商品添加成功，id为{}", good.getGoods_id());
    }

    @Override
    public Result qSearch(String query) {
        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Good::getGoods_name, query);
        queryWrapper.last("limit 20");

        List<Good> goodList = goodMapper.selectList(queryWrapper);
        List<QSearchGoodVo> qSearchGoodVoList = new ArrayList<>();

        for (Good good : goodList) {
            QSearchGoodVo qSearchGoodVo = new QSearchGoodVo();
            BeanUtils.copyProperties(good, qSearchGoodVo);
            qSearchGoodVoList.add(qSearchGoodVo);
        }

        return Result.success(qSearchGoodVoList);

    }

    @Override
    public Result search(GoodParam goodParam) {
        //long start = System.currentTimeMillis();

        Page<Good> goodPage = new Page<>(goodParam.getPagenum(), goodParam.getPagesize());
        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Good.class, info -> !info.getColumn().equals("goods_introduce"));
        if (ObjectUtils.isNotNull(goodParam.getQuery())) {
            queryWrapper.like(Good::getGoods_name, goodParam.getQuery());
        }

        if (ObjectUtils.isNotNull(goodParam.getCid())) {
            queryWrapper.eq(Good::getCat_id, goodParam.getCid());
        }

        Page<Good> page = goodMapper.selectPage(goodPage, queryWrapper);

       // log.info("查到商品基本信息用时：{}ms", System.currentTimeMillis() - start);
        List<Good> records = page.getRecords();
        long total = page.getTotal();
        long current = page.getCurrent();

        //long l = System.currentTimeMillis();
        List<CateGoodVo> cateGoodVos = new ArrayList<>();
        for (Good record : records) {
            CateGoodVo cateGoodVo = new CateGoodVo();
            BeanUtils.copyProperties(record, cateGoodVo);

            List<GoodPics> goodPics = goodPicsService.selectByGid(record.getGoods_id());
            if (ObjectUtils.isNotNull(goodPics)) {
                GoodPics goodPics1 = goodPics.get(0);
                cateGoodVo.setGoods_big_logo(goodPics1.getPics_big_url());
                cateGoodVo.setGoods_small_logo(goodPics1.getPics_sma_url());
            }

            cateGoodVos.add(cateGoodVo);
        }
       // log.info("查完所有商品图片用时：{}ms", System.currentTimeMillis() - l);

        SearchGoodVo searchGoodVo = new SearchGoodVo();
        searchGoodVo.setGoods(cateGoodVos);
        searchGoodVo.setPagenum(current);
        searchGoodVo.setTotal(total);
        //log.info("商品列表查询用时：{}ms", System.currentTimeMillis() - start);

        return Result.success(searchGoodVo);

    }

    @Override
    public Result detail(String goods_id) {
        //long l = System.currentTimeMillis();

        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Good::getGoods_id, goods_id);
        Good good = goodMapper.selectOne(queryWrapper);
        List<GoodPics> goodPics = goodPicsService.selectByGid(Integer.parseInt(goods_id));
        List<GoodAttrs> goodAttrs = goodAttrsService.selectByGid(Integer.parseInt(goods_id));

        GoodVo goodVo = new GoodVo();
        BeanUtils.copyProperties(good, goodVo);
        if (ObjectUtils.isNotNull(goodPics)) {
            for (GoodPics goodPic : goodPics) {
                goodVo.setGoods_big_logo(goodPic.getPics_big_url());
                goodVo.setGoods_small_logo(goodPic.getPics_sma_url());
                break;
            }
        }

        goodVo.setPics(goodPics);
        goodVo.setAttrs(goodAttrs);

        //log.info("商品详情查询用时：{}ms", System.currentTimeMillis() - l);

        return Result.success(goodVo);

    }
}
